﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GEA
{

    public partial class frmDUs : Form
    {

        public string Quant = "";
        //private mdiMain mainForm = null;
        public Processo Proc;
        clDatabase dal = new clDatabase();
        public String Accao = "";

        public frmDUs( Processo proc)
        {
            InitializeComponent();
            Proc = proc;         
        }
        public frmDUs(Processo proc, string accao)
        {
            InitializeComponent();
            Proc = proc;
            Accao = accao;
        }
        public frmDUs()
        {
            InitializeComponent();
        }

        private void frmDUs_Load(object sender, EventArgs e)
        {

            dtpProcesso.Format = DateTimePickerFormat.Custom;
            dtpProcesso.CustomFormat = "yyyy-MM-dd";
            dtpContraMarca.Format = DateTimePickerFormat.Custom;
            dtpContraMarca.CustomFormat = "yyyy-MM-dd";
            dtpData.CustomFormat = "yyyy-MM-dd";
            dtpData.Format = DateTimePickerFormat.Custom;

            int h_2 = (((mdiMain)this.ParentForm).Height - 200 ) / 2;
            panel1.Height = h_2 - 60;
            gbProcesso.Height = h_2 - 20;

            panel2.Height = h_2;
            panel2.Location = new Point(panel2.Location.X, h_2);
            lDUs.Location = new Point(panel2.Location.X, 10);
            dgvDUs.Height = h_2 - 50 ;
            dgvMovimentos.Height = h_2 - 50;
            dgvAddMovimentos.Height = h_2 - 50;
            gbDU.Height = h_2 - 50; 

            int w = panel2.Width;
            dgvDUs.Width = w / 2 - 25;
            gbDU.Width = w / 2 - 25;
            gbDU.Text = "";

            dgvMovimentos.Location = new Point(w / 2 + 5, dgvMovimentos.Location.Y);
            dgvMovimentos.Width = w / 2 - 25;
           

            dgvAddMovimentos.Location = new Point(w / 2 + 5, dgvMovimentos.Location.Y);
            lMercadorias.Location = new Point(w / 2 + 5, lMercadorias.Location.Y);
            dgvAddMovimentos.Width = w / 2 - 25;


            this.Padding = new System.Windows.Forms.Padding(5, 20, 0, 50);

            



            if (Accao == "editar")
            {
                gbProcesso.Enabled = true;
                tbProcesso.Enabled = true;
                dtpProcesso.Enabled = true;
                tbFornecedor.Enabled = true;
                tbFactura.Enabled = true;
                tbContraMarca.Enabled = true;
                dtpContraMarca.Enabled = true;

            }
            else
            {
                if (Accao == "novo")
                {

                    gbProcesso.Enabled = true;
                    tbProcesso.Enabled = true;
                    dtpProcesso.Enabled = true;
                    tbFornecedor.Enabled = true;
                    tbFactura.Enabled = true;
                    tbContraMarca.Enabled = true;
                    dtpContraMarca.Enabled = true;                   

                    gbProcesso.Visible = true;
                    gbDU.Visible = true;
                    dgvDUs.Visible = false;
                    dgvMovimentos.Visible = false;
                    dgvAddMovimentos.Visible = true;



                    dgvAddMovimentos.ColumnCount = 5;
                    dgvAddMovimentos.Columns[0].HeaderText = "REFERÊNCIA".ToUpper();
                    dgvAddMovimentos.Columns[1].HeaderText = "QUANTIDADE".ToUpper();
                    dgvAddMovimentos.Columns[2].HeaderText = "VALOR".ToUpper();
                    dgvAddMovimentos.Columns[3].HeaderText = "PESO BRUTO".ToUpper();
                    dgvAddMovimentos.Columns[4].HeaderText = "PESO LIQUIDO".ToUpper();

                   

                    //dgvAddMovimentos.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
                    //dgvAddMovimentos.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dgvAddMovimentos.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
                    

                    tbIdDU.Focus();

                   
                    tbProcesso.BackColor = System.Drawing.Color.White;
                    dtpProcesso.BackColor = System.Drawing.Color.White;
                    tbFornecedor.BackColor = System.Drawing.Color.White;
                    tbFactura.BackColor = System.Drawing.Color.White;
                    tbContraMarca.BackColor = System.Drawing.Color.White;
                    dtpContraMarca.BackColor = System.Drawing.Color.White;

                    gbProcesso.Text = Proc.Id_entreposto;
                    tbProcesso.Text = Proc.Id_processo;
                    dtpProcesso.Value = Proc.Data_processo;
                    tbFornecedor.Text = Proc.Fornecedor;
                    tbFactura.Text = Proc.Id_factura;
                    tbContraMarca.Text = Proc.Contra_marca;
                    dtpContraMarca.Value = Proc.Data_Contra_marca;

                    if(!string.IsNullOrEmpty(Proc.Id_entreposto)) 
                    {
                        tbProcesso.Enabled = false;
                        dtpProcesso.Enabled = false;
                        tbFornecedor.Enabled = false;
                        tbFactura.Enabled = false;
                        tbContraMarca.Enabled = false;
                        dtpContraMarca.Enabled = false;

                    }



                }
                else
                {
                    if (Accao == "ver")
                    {
                        tbProcesso.Enabled = false;
                        dtpProcesso.Enabled = false;
                        tbFornecedor.Enabled = false;
                        tbFactura.Enabled = false;
                        tbContraMarca.Enabled = false;
                        dtpContraMarca.Enabled = false;

                        tbProcesso.BackColor = System.Drawing.Color.White;
                        dtpProcesso.BackColor = System.Drawing.Color.White;
                        tbFornecedor.BackColor = System.Drawing.Color.White;
                        tbFactura.BackColor = System.Drawing.Color.White;
                        tbContraMarca.BackColor = System.Drawing.Color.White;
                        dtpContraMarca.BackColor = System.Drawing.Color.White;

                        gbProcesso.Text = Proc.Id_entreposto;
                        tbProcesso.Text = Proc.Id_processo;
                        dtpProcesso.Value = Proc.Data_processo;
                        tbFornecedor.Text = Proc.Fornecedor;
                        tbFactura.Text = Proc.Id_factura;
                        tbContraMarca.Text = Proc.Contra_marca;
                        dtpContraMarca.Value = Proc.Data_Contra_marca;

                        dgvMovimentos.Visible = true;

                       

                        Load_DUs();
                    }

                }
            }

                      

            //cores das grid's

            dgvMovimentos.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
            dgvMovimentos.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvMovimentos.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
            //dgvMovimentos.Columns[0].HeaderCell.Style.Font = new Font("Arial", 14F, FontStyle.Bold, GraphicsUnit.Pixel);

            //dgvAddMovimentos.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
            //dgvAddMovimentos.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //dgvAddMovimentos.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
            ////dgvAddMovimentos.Columns[0].HeaderCell.Style.Font = new Font("Arial", 14F, FontStyle.Bold, GraphicsUnit.Pixel);
            
        
        }


        public void Load_DUs(Processo Obj_proc = null)
        {

            if (string.IsNullOrEmpty(Proc.Id_entreposto))
            {
                Proc = Obj_proc;
            }
            //dgvDUs.Columns.Clear();
            //dgvDUs.DataSource = null;


            dgvDUs.DataSource = dal.DU_Get_Lista_dt(Proc.Id_entreposto, Proc.Id_processo);
            if (dgvDUs.RowCount > 0)
            {
                gbProcesso.Visible = true;
                gbDU.Visible = false;
                dgvDUs.Visible = true;
                dgvMovimentos.Visible = true;
                dgvAddMovimentos.Visible = false;


                dgvDUs.Columns["id_user"].Visible = false;
                dgvDUs.Columns["data_criacao"].Visible = false;
                dgvDUs.Columns["data_alteracao"].Visible = false;
                dgvDUs.Columns["id_entreposto"].Visible = false;
                dgvDUs.Columns["id_processo"].Visible = false;
                dgvDUs.Columns["activo"].Visible = false;
                dgvDUs.Columns["du_entrada"].Visible = false;
                dgvDUs.Columns["guid"].Visible = false;

                if (int.Parse(GlobalVar.GlobalValue.ToString()) == 0)
                {
                    dgvDUs.Columns["activo"].Visible = true;
                    dgvDUs.Columns["id_user"].HeaderText = "UTILIZADOR".ToUpper();
                    dgvDUs.Columns["data_criacao"].HeaderText = "DATA CRIAÇÃO".ToUpper();
                    dgvDUs.Columns["data_alteracao"].HeaderText = "DATA ALTERAÇÃO".ToUpper();
                    dgvDUs.Columns["id_entreposto"].HeaderText = "Nº ENTREPOSTO".ToUpper();
                    dgvDUs.Columns["id_processo"].HeaderText = "Nº PROCESSO".ToUpper();

                    dgvDUs.Columns["du_entrada"].HeaderText = "DU ENTRADA".ToUpper();
                    dgvDUs.Columns["activo"].HeaderText = "ACTIVO".ToUpper();
                }
                dgvDUs.Columns["id_du"].HeaderText = "Nº DU".ToUpper();
                dgvDUs.Columns["data_du"].HeaderText = "DATA DU".ToUpper();
                dgvDUs.Columns["alfandega"].HeaderText = "ALFÂNDEGA".ToUpper();
                dgvDUs.Columns["regime"].HeaderText = "REGIME".ToUpper();
                //dgvDUs.Columns["regime"].DisplayIndex = 1;





                //cores das grid's
                dgvDUs.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
                dgvDUs.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgvDUs.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
                //dgvDUs.Columns[0].HeaderCell.Style.Font = new Font("Arial", 14F, FontStyle.Bold, GraphicsUnit.Pixel);

                Load_Mercadoria();

                if (!dgvDUs.Columns.Contains("btn"))
                {
                    DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
                    dgvDUs.Columns.Add(btn);
                    
                    btn.HeaderText = "DU's";
                    btn.Text = "Imprimir";
                    btn.Name = "btn";
                    btn.UseColumnTextForButtonValue = true;
                    //dgvDUs.Columns["btn"].DisplayIndex = 20;
                }


                
            }
            else
            {
                dgvMovimentos.DataSource = null;
            }

        }



           


       
        public void Load_Mercadoria()
        {

            DataGridViewCell cell = null;
            foreach (DataGridViewCell selectedCell in dgvDUs.SelectedCells)
            {
                cell = selectedCell;
                break;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                string id_e = row.Cells["id_entreposto"].Value.ToString();
                string id_p = row.Cells["id_processo"].Value.ToString();
                string id_du = row.Cells["id_du"].Value.ToString();
                Boolean activo = Boolean.Parse( row.Cells["activo"].Value.ToString());
                DateTime data_criacao = DateTime.Parse(row.Cells["data_criacao"].Value.ToString());

                if (GlobalVar.GlobalValue == 0)
                {
                    dgvMovimentos.DataSource = dal.Mercadorias_Get_Lista_dt(id_e, id_p, id_du, activo, data_criacao);
                }
                else
                {
                    dgvMovimentos.DataSource = dal.Mercadorias_Get_Lista_dt(id_e, id_p, id_du, activo);
                }
                dgvMovimentos.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvMovimentos.Columns["id_user"].Visible = false;
                dgvMovimentos.Columns["data_criacao"].Visible = false;
                dgvMovimentos.Columns["data_alteracao"].Visible = false;
                dgvMovimentos.Columns["id_entreposto"].Visible = false;
                dgvMovimentos.Columns["id_processo"].Visible = false;
                dgvMovimentos.Columns["id_du"].Visible = false;
                dgvMovimentos.Columns["id_referencia"].HeaderText = "REFERÊNCIA".ToUpper();
                dgvMovimentos.Columns["id_referencia"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

                dgvMovimentos.Columns["quantidade"].HeaderText = "QUANTIDADE".ToUpper();
                dgvMovimentos.Columns["valor"].HeaderText = "VALOR".ToUpper();
                dgvMovimentos.Columns["valor"].DefaultCellStyle.Format = "N2";                
                dgvMovimentos.Columns["peso_bruto"].HeaderText = "PESO BRUTO".ToUpper();
                dgvMovimentos.Columns["peso_bruto"].DefaultCellStyle.Format = "N2";
                dgvMovimentos.Columns["peso_liquido"].HeaderText = "PESO LIQUIDO".ToUpper();
                dgvMovimentos.Columns["peso_liquido"].DefaultCellStyle.Format = "N2";
                dgvMovimentos.Columns["saldo"].HeaderText = "SALDO".ToUpper();



                dgvMovimentos.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
                dgvMovimentos.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgvMovimentos.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
                
            }
            else
            {
                dgvMovimentos.DataSource = null;
            }
            //cores das grid's
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            Load_Mercadoria();

        }
       

        private void dgvDUs_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(e.ColumnIndex.ToString());
            if (e.ColumnIndex == 0)
            {
                //MessageBox.Show((e.RowIndex + 1) + "  Row  " + (e.ColumnIndex + 1) + "  Column button clicked ");
                DataGridViewCell cell = null;
                foreach (DataGridViewCell selectedCell in dgvDUs.SelectedCells)
                {
                    cell = selectedCell;
                    break;
                }
                if (cell != null)
                {
                    DataGridViewRow row = cell.OwningRow;
                    
                    string id_d = row.Cells["id_du"].Value.ToString();
                    string id_p = row.Cells["id_processo"].Value.ToString();

                    if (((mdiMain)this.ParentForm).jReports == null)
                    {
                        ((mdiMain)this.ParentForm).jReports = new frmReports(GlobalVar.GlobalEntreposto.ToString());
                        ((mdiMain)this.ParentForm).jReports.MdiParent = this.ParentForm;
                        ((mdiMain)this.ParentForm).jReports.Dock = DockStyle.Fill;
                        ((mdiMain)this.ParentForm).jReports.Show();
                    }
                    else
                    {
                        ((mdiMain)this.ParentForm).tsReport1.Enabled = true;
                        ((mdiMain)this.ParentForm).tsReport2.Enabled = true;
                        ((mdiMain)this.ParentForm).tsReport3.Enabled = true;
                        ((mdiMain)this.ParentForm).tsReport4.Enabled = true;
                        ((mdiMain)this.ParentForm).jReports.Activate();
                        ((mdiMain)this.ParentForm).jReports.Load_DGV(GlobalVar.GlobalEntreposto, "");

                    }

                    ((mdiMain)this.ParentForm).Report2(id_p,id_d);
                }
            }
        }


        private void dgvAddMovimentos_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.HeaderText.ToUpper();
        }

        private void dgvDUs_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.HeaderText.ToUpper();
        }

        private void dgvMovimentos_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.HeaderText.ToUpper();
        }

        private void dGVProcessos_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.HeaderText.ToUpper();
        }

        private void dgvAddMovimentos_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            Quant = "";
        }




    }
}
